<template>
  <div class="facility">
    <module
      ref="module"
      :moduleType="inspectionCompletionMonth.moduleType"
      :borderConfig="inspectionCompletionMonth.borderConfig"
      :moduleConfig="inspectionCompletionMonth.moduleConfig"></module>
    <module
      ref="module1"
      :moduleType="inspectionTimelyMonth.moduleType"
      :borderConfig="inspectionTimelyMonth.borderConfig"
      :moduleConfig="inspectionTimelyMonth.moduleConfig"></module>
    <module
      ref="module2"
      :borderConfig="inspectionAbnormalMonth.borderConfig"
      :moduleConfig="inspectionAbnormalMonth.moduleConfig"></module>
    <module
      ref="module3"
      :borderConfig="maintainCompletionMonth.borderConfig"
      :moduleConfig="maintainCompletionMonth.moduleConfig"></module>
    <module
      ref="module4"
      :borderConfig="maintainTimelyMonth.borderConfig"
      :moduleConfig="maintainTimelyMonth.moduleConfig"></module>
    <module
      ref="module5"
      :borderConfig="maintainAbnormalMonth.borderConfig"
      :moduleConfig="maintainAbnormalMonth.moduleConfig"></module>
    <module
      ref="module6"
      :borderConfig="inspectionOrgan.borderConfig"
      :moduleConfig="inspectionOrgan.moduleConfig"
      :dimensionArr="inspectionOrgan.dimensionArr"
      :isShowDimension="inspectionOrgan.isShowDimension"></module>
    <module
      ref="module7"
      :borderConfig="maintainOrgan.borderConfig"
      :moduleConfig="maintainOrgan.moduleConfig"
      :isShowDimension="maintainOrgan.isShowDimension"></module>
  </div>
</template>

<script>
import module from '../components/SecondaryModule'
import { changeBarData, separateData } from './../../utils/index'
export default {
  components: {
    module
  },
  computed: {
    organId () {
      return this.$store.state.organId
    }
  },
  name: 'facility',
  data () {
    return {
      // 巡检完成率趋势配置
      inspectionCompletionMonth: {
        borderConfig: {
          moduleType: '43x1',
          icon: '',
          titleText: '巡检完成率趋势',
          ref: 'inspectionCompletionMonth'
        },
        moduleConfig: {
          optionsConfig: {
            axis: {
              yAxisConversion: '%', // Y轴坐标%
              xAxisDataType: 2 // X轴坐标从1月份开始
            },
            yAxis: {
              max: 100
            },
            tooltipsConfig: {
              symbol: '%',
              isNamePreposition: false,
              tipsMessage: '巡检完成率'
            },
            grid: {
              top: 40
            },
            seriesData: []
          },
          legendList: [{ name: new Date().getFullYear() - 1, colorIndex: 0 }, { name: new Date().getFullYear(), colorIndex: 1 }],
          legendDirection: 'level'
        }
      },
      // 巡检及时率趋势配置
      inspectionTimelyMonth: {
        borderConfig: {
          moduleType: '43x1',
          icon: '',
          titleText: '巡检及时率趋势',
          ref: 'inspectionTimelyMonth'
        },
        moduleConfig: {
          optionsConfig: {
            axis: {
              yAxisConversion: '%', // Y轴坐标%
              xAxisDataType: 2 // X轴坐标从1月份开始
            },
            yAxis: {
              max: 100
            },
            tooltipsConfig: {
              symbol: '%',
              isNamePreposition: false,
              tipsMessage: '巡检及时率'
            },
            grid: {
              top: 40
            },
            seriesData: []
          },
          legendList: [{ name: new Date().getFullYear() - 1, colorIndex: 0 }, { name: new Date().getFullYear(), colorIndex: 1 }],
          legendDirection: 'level'
        }
      },
      // 巡检异常率趋势配置
      inspectionAbnormalMonth: {
        borderConfig: {
          titleText: '巡检异常率趋势',
          moduleType: '43x1',
          ref: 'inspectionAbnormalMonth'
        },
        moduleConfig: {
          optionsConfig: {
            axis: {
              yAxisConversion: '%', // Y轴坐标%
              xAxisDataType: 2 // X轴坐标从1月份开始
            },
            yAxis: {
              max: 100
            },
            tooltipsConfig: {
              symbol: '%',
              isNamePreposition: false,
              tipsMessage: '巡检异常率'
            },
            grid: {
              top: 40
            },
            seriesData: []
          },
          legendDirection: 'level',
          legendList: [{ name: new Date().getFullYear() - 1, colorIndex: 0 }, { name: new Date().getFullYear(), colorIndex: 1 }]
        }
      },
      // 保养完成率趋势趋势
      maintainCompletionMonth: {
        borderConfig: {
          titleText: '保养完成率趋势',
          moduleType: '43x1',
          ref: 'maintainCompletionMonth'
        },
        moduleConfig: {
          optionsConfig: {
            axis: {
              yAxisConversion: '%', // Y轴坐标%
              xAxisDataType: 2 // X轴坐标从1月份开始
            },
            yAxis: {
              max: 100
            },
            tooltipsConfig: {
              symbol: '%',
              isNamePreposition: false,
              tipsMessage: '保养完成率'
            },
            grid: {
              top: 40
            },
            seriesData: []
          },
          legendDirection: 'level',
          legendList: [{ name: new Date().getFullYear() - 1, colorIndex: 0 }, { name: new Date().getFullYear(), colorIndex: 1 }]
        }
      },
      // 保养及时率趋势
      maintainTimelyMonth: {
        borderConfig: {
          titleText: '保养及时率趋势',
          moduleType: '43x1',
          ref: 'maintainTimelyMonth'
        },
        moduleConfig: {
          optionsConfig: {
            axis: {
              yAxisConversion: '%', // Y轴坐标%
              xAxisDataType: 2 // X轴坐标从1月份开始
            },
            yAxis: {
              max: 100
            },
            tooltipsConfig: {
              symbol: '%',
              isNamePreposition: false,
              tipsMessage: '保养及时率'
            },
            grid: {
              top: 40
            },
            seriesData: []
          },
          legendDirection: 'level',
          legendList: [{ name: new Date().getFullYear() - 1, colorIndex: 0 }, { name: new Date().getFullYear(), colorIndex: 1 }]
        }
      },
      // 保养异常率趋势
      maintainAbnormalMonth: {
        borderConfig: {
          titleText: '保养异常率趋势',
          moduleType: '43x1',
          ref: 'maintainAbnormalMonth'
        },
        moduleConfig: {
          optionsConfig: {
            axis: {
              yAxisConversion: '%', // Y轴坐标%
              xAxisDataType: 2 // X轴坐标从1月份开始
            },
            yAxis: {
              max: 100
            },
            tooltipsConfig: {
              symbol: '%',
              isNamePreposition: false,
              tipsMessage: '保养异常率'
            },
            grid: {
              top: 40
            },
            seriesData: []
          },
          legendDirection: 'level',
          legendList: [{ name: new Date().getFullYear() - 1, colorIndex: 0 }, { name: new Date().getFullYear(), colorIndex: 1 }]
        }
      },
      // 巡检指标对比
      inspectionOrgan: {
        borderConfig: {
          titleText: '巡检指标对比',
          moduleType: '2x1',
          ref: 'inspectionOrgan'
        },
        moduleConfig: {
          optionsConfig: {
            axis: {
              xAxisNameLength: 8,
              yAxisConversion: '%' // Y轴坐标%
            },
            xAxis: {
              axisLabel: {
                rotate: 30
              },
              data: []
            },
            yAxis: {
              max: 100
            },
            tooltipsConfig: {
              symbol: '%',
              isNamePreposition: true
            },
            grid: {
              top: 40,
              bottom: 0
            },
            dataZoom: {
              show: true,
              left: '60%'
            },
            seriesData: []
          },
          legendClassName: true,
          legendDirection: 'level',
          legendList: [{ name: '巡检完成率', colorIndex: 0 }, { name: '巡检及时率', colorIndex: 1 }, { name: '巡检异常率', colorIndex: 2 }]
        },
        isShowDimension: true
      },
      // 保养指标对比
      maintainOrgan: {
        borderConfig: {
          titleText: '保养指标对比',
          moduleType: '2x1',
          ref: 'maintainOrgan'
        },
        moduleConfig: {
          optionsConfig: {
            axis: {
              xAxisNameLength: 8,
              yAxisConversion: '%' // Y轴坐标%
            },
            xAxis: {
              axisLabel: {
                rotate: 30
              },
              data: []
            },
            yAxis: {
              max: 100
            },
            tooltipsConfig: {
              symbol: '%',
              isNamePreposition: true
            },
            grid: {
              top: 40,
              bottom: 0
            },
            dataZoom: {
              show: true,
              left: '60%'
            },
            seriesData: []
          },
          legendClassName: true,
          legendDirection: 'level',
          legendList: [{ name: '保养完成率', colorIndex: 0 }, { name: '保养及时率', colorIndex: 1 }, { name: '保养异常率', colorIndex: 2 }]
        },
        isShowDimension: true
      }
    }
  },
  methods: {
    showTips (data) {
      this.$parent.showTips(data)
    },
    // 改变时间维度时
    changeDimension (obj) {
      switch (obj.ref) {
        case 'inspectionOrgan': this.getDtRateByTypeAndCompany('1000', obj.index + 1)
          break
        case 'maintainOrgan': this.getDtRateByTypeAndCompany('2000', obj.index + 1)
          break
      }
    },
    // 设备巡检/保养指标年度对比
    getDtFieldContrastsByYear (type = '1000') {
      this.equipmentInfoApi.getDtFieldContrastsByYear({ organId: this.organId, dtType: type }).then(res => {
        let data1 = [] // 完成率
        let data2 = [] // 及时率
        let data3 = [] // 异常率
        if (res.data.code === '0') {
          data1 = changeBarData(res.data.data, 'finishRate')
          data2 = changeBarData(res.data.data, 'timelyRate')
          data3 = changeBarData(res.data.data, 'abnormalRate')
        } else {
          this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
        }
        if (type === '1000') {
          this.inspectionCompletionMonth.moduleConfig.optionsConfig.seriesData = [
            {
              data: data1[0],
              name: new Date().getFullYear() - 1 + '年',
              colorIndex: 0
            },
            {
              data: data1[1],
              name: new Date().getFullYear() + '年',
              colorIndex: 1
            }
          ]
          this.inspectionTimelyMonth.moduleConfig.optionsConfig.seriesData = [
            {
              data: data2[0],
              name: new Date().getFullYear() - 1 + '年',
              colorIndex: 0
            },
            {
              data: data2[1],
              name: new Date().getFullYear() + '年',
              colorIndex: 1
            }
          ]
          this.inspectionAbnormalMonth.moduleConfig.optionsConfig.seriesData = [
            {
              data: data3[0],
              name: new Date().getFullYear() - 1 + '年',
              colorIndex: 0
            },
            {
              data: data3[1],
              name: new Date().getFullYear() + '年',
              colorIndex: 1
            }
          ]
        } else {
          this.maintainCompletionMonth.moduleConfig.optionsConfig.seriesData = [
            {
              data: data1[0],
              name: new Date().getFullYear() - 1 + '年',
              colorIndex: 0
            },
            {
              data: data1[1],
              name: new Date().getFullYear() + '年',
              colorIndex: 1
            }
          ]
          this.maintainTimelyMonth.moduleConfig.optionsConfig.seriesData = [
            {
              data: data2[0],
              name: new Date().getFullYear() - 1 + '年',
              colorIndex: 0
            },
            {
              data: data2[1],
              name: new Date().getFullYear() + '年',
              colorIndex: 1
            }
          ]
          this.maintainAbnormalMonth.moduleConfig.optionsConfig.seriesData = [
            {
              data: data3[0],
              name: new Date().getFullYear() - 1 + '年',
              colorIndex: 0
            },
            {
              data: data3[1],
              name: new Date().getFullYear() + '年',
              colorIndex: 1
            }
          ]
        }
      }, res => {
        if (type === '1000') {
          this.inspectionCompletionMonth.moduleConfig.optionsConfig.seriesData = [
            {
              data: [],
              name: new Date().getFullYear() - 1 + '年',
              colorIndex: 0
            },
            {
              data: [],
              name: new Date().getFullYear() + '年',
              colorIndex: 1
            }
          ]
          this.inspectionTimelyMonth.moduleConfig.optionsConfig.seriesData = [
            {
              data: [],
              name: new Date().getFullYear() - 1 + '年',
              colorIndex: 0
            },
            {
              data: [],
              name: new Date().getFullYear() + '年',
              colorIndex: 1
            }
          ]
          this.inspectionAbnormalMonth.moduleConfig.optionsConfig.seriesData = [
            {
              data: [],
              name: new Date().getFullYear() - 1 + '年',
              colorIndex: 0
            },
            {
              data: [],
              name: new Date().getFullYear() + '年',
              colorIndex: 1
            }
          ]
        } else {
          this.maintainCompletionMonth.moduleConfig.optionsConfig.seriesData = [
            {
              data: [],
              name: new Date().getFullYear() - 1 + '年',
              colorIndex: 0
            },
            {
              data: [],
              name: new Date().getFullYear() + '年',
              colorIndex: 1
            }
          ]
          this.maintainTimelyMonth.moduleConfig.optionsConfig.seriesData = [
            {
              data: [],
              name: new Date().getFullYear() - 1 + '年',
              colorIndex: 0
            },
            {
              data: [],
              name: new Date().getFullYear() + '年',
              colorIndex: 1
            }
          ]
          this.maintainAbnormalMonth.moduleConfig.optionsConfig.seriesData = [
            {
              data: [],
              name: new Date().getFullYear() - 1 + '年',
              colorIndex: 0
            },
            {
              data: [],
              name: new Date().getFullYear() + '年',
              colorIndex: 1
            }
          ]
        }
        this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
      })
    },
    // 设备巡检/保养分公司对比
    getDtRateByTypeAndCompany (type, timeDimensionIndex) {
      let timeDimension = 1
      if (timeDimensionIndex) {
        timeDimension = timeDimensionIndex
      }
      this.equipmentInfoApi.getDtRateByTypeAndCompany({ organId: this.organId, timeDimension: timeDimension, dtType: type }).then(res => {
        let data1 = []
        let data2 = []
        let data3 = []
        let name = []
        if (res.data.code === '0') {
          name = separateData(res.data.data, 'finishRate').tipsName
          data1 = separateData(res.data.data, 'finishRate').data
          data2 = separateData(res.data.data, 'timelyRate').data
          data3 = separateData(res.data.data, 'abnormalRate').data
        } else {
          this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
        }
        if (type === '1000') {
          this.inspectionOrgan.moduleConfig.optionsConfig.xAxis.data = name
          this.inspectionOrgan.moduleConfig.optionsConfig.seriesData = [
            {
              data: data1,
              name: '巡检完成率',
              colorIndex: 0,
              isBar: true
            },
            {
              data: data2,
              name: '巡检及时率',
              colorIndex: 1,
              isBar: true
            },
            {
              data: data3,
              name: '巡检异常率',
              colorIndex: 2,
              isBar: true
            }
          ]
        } else {
          this.maintainOrgan.moduleConfig.optionsConfig.xAxis.data = name
          this.maintainOrgan.moduleConfig.optionsConfig.seriesData = [
            {
              data: data1,
              name: '保养完成率',
              colorIndex: 0,
              isBar: true
            },
            {
              data: data2,
              name: '保养及时率',
              colorIndex: 1,
              isBar: true
            },
            {
              data: data3,
              name: '保养异常率',
              colorIndex: 2,
              isBar: true
            }
          ]
        }
      }, res => {
        if (type === '1000') {
          this.inspectionOrgan.moduleConfig.optionsConfig.xAxis.data = []
          this.inspectionOrgan.moduleConfig.optionsConfig.seriesData = [
            {
              data: [],
              name: '巡检完成率',
              colorIndex: 0,
              isBar: true
            },
            {
              data: [],
              name: '巡检及时率',
              colorIndex: 1,
              isBar: true
            },
            {
              data: [],
              name: '巡检异常率',
              colorIndex: 2,
              isBar: true
            }
          ]
        } else {
          this.maintainOrgan.moduleConfig.optionsConfig.xAxis.data = []
          this.maintainOrgan.moduleConfig.optionsConfig.seriesData = [
            {
              data: [],
              name: '保养完成率',
              colorIndex: 0,
              isBar: true
            },
            {
              data: [],
              name: '保养及时率',
              colorIndex: 1,
              isBar: true
            },
            {
              data: [],
              name: '保养异常率',
              colorIndex: 2,
              isBar: true
            }
          ]
        }
        this.showTips({ showMessage: '请求失败，点击确认继续刷新', sureCallback: this.getData })
      })
    },
    // 请求数据
    getData () {
      this.getDtFieldContrastsByYear('1000')
      this.getDtFieldContrastsByYear('2000')
      this.getDtRateByTypeAndCompany('1000')
      this.getDtRateByTypeAndCompany('2000')
    },
    resizeChart () {
      this.$refs.module.resizeChart()
      this.$refs.module1.resizeChart()
      this.$refs.module2.resizeChart()
      this.$refs.module3.resizeChart()
      this.$refs.module4.resizeChart()
      this.$refs.module5.resizeChart()
      this.$refs.module6.resizeChart()
      this.$refs.module7.resizeChart()
    }
  },
  mounted () {
    // this.getData()
  }
}
</script>

<style lang="scss" scoped>
  .facility{
    width: calc(100% - 10px);
    position: relative;
    margin: 5px;
    overflow: hidden;
    moz-user-select: -moz-none;
    -moz-user-select: none;
    -o-user-select:none;
    -khtml-user-select:none;
    -webkit-user-select:none;
    -ms-user-select:none;
    user-select:none;
  }
</style>
